package com.hbsx.why.base;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.hbsx.why.R;
import com.hbsx.why.utils.LogUtils;
import com.hbsx.why.utils.StatusBarCompat;
import com.hbsx.why.utils.ToastUtils;
import com.hbsx.why.utils.changeskin.SkinManager;
import com.squareup.leakcanary.RefWatcher;

import butterknife.ButterKnife;

/**
 * @author LIUB
 * @desc 封装的基础activity
 * @time 2017/6/26 13:30
 */

public abstract class BaseActivity extends AppCompatActivity {
    private Context context = this;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        //SkinManager.getInstance().register(this);
        super.onCreate(savedInstanceState);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            // Translucent status bar
            window.setFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
//        StatusBarCompat.compat(this, Color.parseColor("#3F51B5"));
        setContentView(getLayoutResId());
//        setContentView();
        ButterKnife.bind(this);
        initView();
        LogUtils.logi("------LocalClassName=" + getLocalClassName());
//        setStatusTint();
    }

    /**
     * 返回当前Activity布局文件的id
     */

    abstract protected int getLayoutResId();

    /**
     * 初始化基本控件
     */
    abstract protected void initView();

    public Context getContext() {
        return context;
    }


    public void launch(Class clazz) {
        startActivity(new Intent(this, clazz));
    }

    public void toast_short(Object obj) {
        Toast.makeText(this, obj + "", Toast.LENGTH_SHORT).show();
    }

    public void toast_long(Object obj) {
        Toast.makeText(this, obj + "", Toast.LENGTH_LONG).show();
    }
    public void showToast(Object obj) {
        ToastUtils.toastShort(""+obj);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        RefWatcher refWatcher = BaseApplication.getRefWatcher(getContext());
        refWatcher.watch(this);
        //SkinManager.getInstance().unregister(this);
    }

    public BaseApplication myApplication() {
        return BaseApplication.getInstance();
    }

    public void setContentView() {
//**该方法需要在setContentView方法之后调用。重写BaseActivity中的setContentView(int layoutResID )方法，
// 就可以实现全局地设置所有的Activity界面的状态栏颜色，如果项目中某个Activity需要设置不同的状态栏颜色，显式地调用该方法，并传入颜色值即可**
        ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0).setFitsSystemWindows(true);
    }

    //设置状态栏的颜色
    private void setStatusTint() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setTranslucentStatus(true);
        }

//        SystemBarTintManager tintManager = new SystemBarTintManager(this);
//        tintManager.setStatusBarTintEnabled(false);
//        tintManager.setNavigationBarTintEnabled(false);
//        tintManager.setStatusBarTintResource(R.color.red);//通知栏所需颜色

    }

    @TargetApi(19)
    private void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);

    }
}
